package com.mtk.launcher.power.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lark.wifi.util.MyLog;
import com.mtk.launcher.R;
import com.mtk.launcher.activity.BaseActivity;
import com.mtk.launcher.power.bean.TimeEntity;
import com.mtk.launcher.power.bean.TimeLocalEntity;
import com.mtk.launcher.power.util.PowerOnOffUtil;
import com.mtk.launcher.power.view.CustomDatePicker;
import com.mtk.launcher.util.SimpleDateUtil;

public class TimerChangeActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_time_change);
        initPowerOnOff();
        initView();
        initListener();
        getDate();
    }

    private void initListener() {
        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    PowerOnOffUtil powerOnOffUtil;

    private void initPowerOnOff() {
        powerOnOffUtil = new PowerOnOffUtil(TimerChangeActivity.this);
    }

    TextView btn_time_off;
    TextView btn_time_on;
    TextView btn_submit;
    TextView btn_cacel;
    CheckBox ck_mon;
    CheckBox ck_tue;
    CheckBox ck_wed;
    CheckBox ck_thu;
    CheckBox ck_fri;
    CheckBox ck_sta;
    CheckBox ck_sun;
    CheckBox ck_all;
    LinearLayout ll_back;

    private void initView() {
        TextView title = findViewById(R.id.title);
        title.setText(getString(R.string.time_settings));
        ll_back = (LinearLayout) findViewById(R.id.ll_back);
        btn_submit = (TextView) findViewById(R.id.btn_submit);
        btn_cacel = (TextView) findViewById(R.id.btn_cacel);
        btn_submit.setOnClickListener(this);
        btn_cacel.setOnClickListener(this);
        btn_time_on = (TextView) findViewById(R.id.btn_time_on);
        btn_time_off = (TextView) findViewById(R.id.btn_time_off);
        btn_time_on.setOnClickListener(this);
        btn_time_off.setOnClickListener(this);
        ck_mon = (CheckBox) findViewById(R.id.ck_mon);
        ck_tue = (CheckBox) findViewById(R.id.ck_tue);
        ck_wed = (CheckBox) findViewById(R.id.ck_wed);
        ck_thu = (CheckBox) findViewById(R.id.ck_thu);
        ck_fri = (CheckBox) findViewById(R.id.ck_fri);
        ck_sta = (CheckBox) findViewById(R.id.ck_sta);
        ck_sun = (CheckBox) findViewById(R.id.ck_sun);
        ck_all = (CheckBox) findViewById(R.id.ck_all);

        ck_mon.setOnCheckedChangeListener(checkedChangeListener);
        ck_tue.setOnCheckedChangeListener(checkedChangeListener);
        ck_wed.setOnCheckedChangeListener(checkedChangeListener);
        ck_thu.setOnCheckedChangeListener(checkedChangeListener);
        ck_fri.setOnCheckedChangeListener(checkedChangeListener);
        ck_sta.setOnCheckedChangeListener(checkedChangeListener);
        ck_sun.setOnCheckedChangeListener(checkedChangeListener);

        ck_all.setOnClickListener(v -> {
            boolean checked = ck_all.isChecked();
            ck_mon.setChecked(checked);
            ck_tue.setChecked(checked);
            ck_wed.setChecked(checked);
            ck_thu.setChecked(checked);
            ck_fri.setChecked(checked);
            ck_sta.setChecked(checked);
            ck_sun.setChecked(checked);
        });
    }


    private CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            ck_all.setChecked(ck_mon.isChecked() && ck_tue.isChecked() && ck_wed.isChecked() && ck_thu.isChecked() && ck_fri.isChecked() && ck_sta.isChecked() && ck_sun.isChecked());
            btn_submit.setEnabled(ck_mon.isChecked() || ck_tue.isChecked() || ck_wed.isChecked() || ck_thu.isChecked() || ck_fri.isChecked() || ck_sta.isChecked() || ck_sun.isChecked());
        }
    };

    String timeId = "";
    int onHour = 6;
    int onMin = 30;
    int offHour = 18;
    int offMin = 30;

    private void getDate() {
        btn_submit.setText(getString(R.string.text_add));
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_time_off) {
            String offTimeShow = btn_time_off.getText().toString().toString();
            if (TextUtils.isEmpty(offTimeShow)) {
                offTimeShow = "18:30";
            }
            showTimeOffDialog(offTimeShow);
        }
        if (view.getId() == R.id.btn_time_on) {
            String onTimeShow = btn_time_on.getText().toString().toString();
            if (TextUtils.isEmpty(onTimeShow)) {
                onTimeShow = "06:30";
            }
            showTimeOnDialog(onTimeShow);
        }
        if (view.getId() == R.id.btn_submit) {
            addTimeEntityToDb();
        }
        if (view.getId() == R.id.btn_cacel) {
            finish();
        }
    }

    /***
     * 添加数据到书库
     */
    private void addTimeEntityToDb() {
        //添加数据前，先清除数据
        showWaitDialogUtil(true);
        TimeLocalEntity timeDbEntity = new TimeLocalEntity();
        String onTime = btn_time_on.getText().toString().trim();
        String offTime = btn_time_off.getText().toString().trim();
        String timeId = System.currentTimeMillis() + "";
        timeDbEntity.setId(timeId);
        timeDbEntity.setTtOnTime(onTime);
        timeDbEntity.setTtOffTime(offTime);
        timeDbEntity.setTtMon(ck_mon.isChecked() ? "true" : "false");
        timeDbEntity.setTtTue(ck_tue.isChecked() ? "true" : "false");
        timeDbEntity.setTtWed(ck_wed.isChecked() ? "true" : "false");
        timeDbEntity.setTtThu(ck_thu.isChecked() ? "true" : "false");
        timeDbEntity.setTtFri(ck_fri.isChecked() ? "true" : "false");
        timeDbEntity.setTtSat(ck_sta.isChecked() ? "true" : "false");
        timeDbEntity.setTtSun(ck_sun.isChecked() ? "true" : "false");
        boolean isSave = powerOnOffUtil.addTimerDb(timeDbEntity);
        if (isSave) {
            powerOnOffUtil.changePowerOnOffByWorkModel("添加定时开关机时间");
            showWaitDialogUtil(false);
            showToastView("保存成功");
            finish();
            return;
        }
        //保存失败，需要删除当前数据
        powerOnOffUtil.delTimerById(timeId);
        showWaitDialogUtil(false);
        showToastView("保存失败，请重试");
    }

    private void showTimeOnDialog(String onTime) {
        CustomDatePicker customDatePicker = new CustomDatePicker(TimerChangeActivity.this, new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(TimeEntity timeEntity) { // 回调接口，获得选中的时间
                onHour = timeEntity.getHour();
                onMin = timeEntity.getMinite();
                btn_time_on.setText(date(timeEntity.getHour()) + ":" + date(timeEntity.getMinite()));
            }
        }, "2025-01-01 00:00", "2050-12-31 00:00"); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
        customDatePicker.showSpecificTime(true); // 不显示时和分
        customDatePicker.showYearDateDayTime(false);
        customDatePicker.setIsLoop(true); // 不允许循环滚动
        String timeNoe = SimpleDateUtil.formatCurrentDate() + onTime;
        customDatePicker.show(timeNoe);
    }

    /**
     * 显示时间
     */
    private void showTimeOffDialog(String offTime) {
        CustomDatePicker customDatePicker = new CustomDatePicker(TimerChangeActivity.this, new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(TimeEntity timeEntity) { // 回调接口，获得选中的时间
                MyLog.cdl("=======返回得时间===" + timeEntity.toString());
                offHour = timeEntity.getHour();
                offMin = timeEntity.getMinite();
                btn_time_off.setText(date(timeEntity.getHour()) + ":" + date(timeEntity.getMinite()));
            }
        }, "2025-01-01 00:00", "2050-12-31 00:00"); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
        customDatePicker.showSpecificTime(true); // 不显示时和分
        customDatePicker.showYearDateDayTime(false);
        customDatePicker.setIsLoop(true); // 不允许循环滚动
        String timeNoe = SimpleDateUtil.formatCurrentDate() + offTime;
        Log.e("cdl", "timeNoe=" + timeNoe);
        customDatePicker.show(timeNoe);
    }

    public static String date(int data) {
        String hour = String.valueOf(data);
        if (hour.length() == 1) {
            hour = "0" + hour;
        }
        return hour;
    }

}
